home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / dev / gui / gui4cli.lha / Gui4Cli / Ext / Exmpl_src / 3_LVIndent / LVIndent.c next >
Encoding:
C/C++ Source or Header  |  1999-06-16  |  5.3 KB  |  190 lines

  1.  
  2. /****************************************************************
  3. **
  4. **    LVIndent.c - 6/9/98 - dck@hol.gr
  5. **    
  6. **    This program will get a lock on Gui4Cli and then
  7. **    indent the current listview by inserting 3 spaces at the 
  8. **    start of each line.
  9. **
  10. ******************************************************************/
  11. // define this for faster exec calls
  12. #define __USESYSBASE
  13. // some of these may not be needed..
  14. #include <exec/exec.h>
  15. #include <exec/execbase.h>
  16. #include <exec/memory.h>
  17. #include <dos/dosextens.h>
  18. #include <dos/rdargs.h>
  19. #include <dos/dostags.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <dos.h>
  24. #include <proto/dos.h>
  25. #include <proto/exec.h>
  26. #include <graphics/text.h>
  27.  
  28. // Gui4Cli.h include file (assuming it's in the include: dir)
  29. #include <Gui4Cli.h>
  30.  
  31. // prototypes
  32. int sendmsg(UBYTE * , struct g4cmsg * , struct ExecBase * , struct DosLibrary * );
  33. int indentlist(struct GCmain * , UBYTE * , struct ExecBase * , struct DosLibrary * );
  34.  
  35. // ==============================================================
  36. // This is the function where execution starts.
  37. // Lock Gui4Cli, get the *GCmain pointer and call the function 
  38. // which will indent the current listview (end of the file)
  39. // ==============================================================
  40.  
  41. int anyname (void)
  42. {
  43.     // these are the libraries we need
  44.     struct ExecBase *SysBase = (*((struct ExecBase **) 4));
  45.     struct DosLibrary *DOSBase=NULL;
  46.     // this is us..
  47.     struct Process *myself = (struct Process *)(SysBase->ThisTask);
  48.  
  49.     struct MsgPort *myport;
  50.     struct g4cmsg  msg;    // the message we'll use
  51.     struct GCmain  *gc;    // Gui4Cli's main structure
  52.     UBYTE  command[100];    // a temporary buffer
  53.     LONG   ret = 10;    // our return code
  54.       
  55.     // open the dos library or die..
  56.     if (!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 36L)))
  57.     {   myself->pr_Result2 = ERROR_INVALID_RESIDENT_LIBRARY;
  58.         goto endprog;
  59.     }
  60.  
  61.     // this is our proccess's message port
  62.     myport = &myself->pr_MsgPort;
  63.  
  64.     // clear & set up the message structure
  65.     memset ((char *)&msg, 0, sizeof(msg));
  66.     msg.node.mn_ReplyPort = myport;
  67.     msg.node.mn_Length = sizeof(struct g4cmsg);
  68.     msg.magic = 392001; // so that g4c recognises us
  69.  
  70.     // make it a lock message
  71.     msg.type = GM_LOCK;
  72.  
  73.     // send the message - a return of 0 means ok
  74.     // otherwise there was a failure and we exit indicating error
  75.     if (ret = sendmsg ("Gui4Cli", &msg, SysBase, DOSBase)) 
  76.         goto endprog;
  77.  
  78.     // The pointer to Gui4Cli's main structure, is in msg.gcmain
  79.     gc = msg.gcmain;
  80.  
  81.     // make sure that it's valid..
  82.     if (gc && (gc->magic == MM_G4C))
  83.     {
  84.          if (indentlist (gc, "   ", SysBase, DOSBase)) ret = 0;
  85.     }
  86.  
  87.     // *must* unlock Gui4Cli after we're through using it..
  88.     msg.type = GM_UNLOCK;
  89.     sendmsg ("Gui4Cli", &msg, SysBase, DOSBase);
  90.     
  91.     // now send a command to nudge the lv into redrawing itself
  92.     msg.type = GM_COMMAND;
  93.     strcpy (command, "LVMove 0");
  94.     msg.com = command;
  95.     sendmsg ("Gui4Cli", &msg, SysBase, DOSBase);
  96.  
  97.     // that's all..
  98.     endprog:
  99.  
  100.     if (DOSBase) CloseLibrary((struct Library *)DOSBase);
  101.     return (ret);
  102. }
  103.  
  104.  
  105. // ==============================================================
  106. // Send a message, wait for reply.. 
  107. // -->> return 0 for OK or error code otherwise
  108. // - portname = the name of Gui4Cli message port (usually Gui4Cli)
  109. // - msg = the message we received
  110. // note : we pass sysbase & dosbase as arguments, because we 
  111. //        will be using functions in those libararies..
  112. // ==============================================================
  113.  
  114. sendmsg (UBYTE *portname, struct g4cmsg *msg,
  115.      struct ExecBase *SysBase, struct DosLibrary *DOSBase)
  116. {
  117.     struct MsgPort *gcport, *myport;
  118.     
  119.     // get a pointer to our message port (for code clarity)
  120.     myport = msg->node.mn_ReplyPort;
  121.  
  122.     Forbid();
  123.     if (gcport = FindPort(portname))
  124.     {
  125.        PutMsg (gcport, &msg->node);
  126.        Permit();
  127.        // wait for reply..
  128.        WaitPort (myport);
  129.        msg = (struct g4cmsg *)GetMsg (myport);
  130.        // return Gui4Cli's return code (probably 0 meaning OK)
  131.        return (msg->res);
  132.     }
  133.     Permit();
  134.     PutStr ("Could not find port!\n"); 
  135.     return (10);
  136. }
  137.  
  138.  
  139. // ==============================================================
  140. // Indent the listview - return success
  141. // go through all records, delete old, replace with new..
  142. // gcm   = the main Gui4Cli structure
  143. // instr = the string to indent it with
  144. // ==============================================================
  145.  
  146. indentlist (struct GCmain *gcm, UBYTE *instr,
  147.         struct ExecBase *SysBase, struct DosLibrary *DOSBase)
  148. {
  149.     LONG   inlen, totlen;
  150.     struct fulist *fls;
  151.     struct lister *fl, *nextfl;
  152.     UBYTE  *buff;
  153.     
  154.     inlen = strlen(instr);
  155.     
  156.     // get pointer to current listview, checking it..
  157.     if (!(fls = gcm->curlv) || !fls->ls) return (0);
  158.     
  159.     // do the whole list..
  160.     fl = (struct lister *)(fls->ls->lh_Head);
  161.     while (nextfl = (struct lister *)(fl->node.ln_Succ))
  162.     {
  163.        // get the buffer we need.. (NOTE totlen + 1 - always!)
  164.        totlen = inlen + fl->length;
  165.        if (!(buff = (UBYTE *)AllocMem(totlen + 1, MEMF_CLEAR)))
  166.        {   PutStr ("No memory!\n");
  167.            return (0);
  168.        }
  169.     
  170.        // construct the line..
  171.        strcpy (buff, instr);
  172.        strcat (buff, fl->start);
  173.        // replace old line
  174.        FreeMem (fl->start, fl->length + 1); // note +1
  175.        fl->start  = buff;   
  176.        fl->length = totlen;
  177.     
  178.         // adjust the max line length counter (just do it..)
  179.         if (fls->maxlength < fl->length) fls->maxlength = fl->length;
  180.  
  181.        // do next line..
  182.        fl = nextfl;
  183.     }
  184.     
  185.     return (1); // ok..
  186. }
  187.  
  188.  
  189.  
  190.